package com.warm.middle.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

/**
 * @Description websocket message 转 java 对象
 * @Author iyb-liangchuang
 * @Date 2021/6/17 10:54
 */
public class WSDecoder implements Decoder.Text<WSMsg> {
    @Override
    public WSMsg decode(String s) throws DecodeException {
        try {
            WSMsg msg = JSON.parseObject(s, new TypeReference<WSMsg>(){});
            return msg;
        } catch (Exception e){
            System.out.println("message conver to object error...");
            return null;
        }
    }

    @Override
    public boolean willDecode(String s) {
        return false;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {
    }

    @Override
    public void destroy() {
    }
}
